import axios from 'axios'
export class TransManager {
    constructor(options, handlers) {
        this.options = options
        this.progress = null;
        this.bucketName = options.bucketName;
        this.objectName = options.objectName;
        this.transOptions = options.transOptions;
        this.storage = options.storage;
        this.taskCheck = null
        this.uploadUrl = this.storage.ApiUrl;
        this.onData = () => { };
        this.onError = () => { };
        this.onComplete = () => { };
        Object.assign(this, handlers);
    }
    trans() {
        axios.post(this.uploadUrl + 'task/trans', {
            bucketName: this.bucketName,
            objectName: this.objectName,
            ...this.transOptions
        }).then((res) => {
            this.taskId = res.data.data.taskId
            this.getTask()
        }).catch(err => {
            this.onError(err)
        })
    }
    getTask() {
        this.taskCheck = setInterval(() => {
            axios.get(this.uploadUrl + 'task/' + this.taskId).then((res) => {
                if (res.data.data.state == 1) {
                    clearInterval(this.taskCheck)
                    this.onComplete(res.data.data)
                } else {
                    this.onData({ total: { percent: res.data.data.progress } })
                }
            }).catch(err => {
                clearInterval(this.taskCheck)
                this.onError(err)
            })
        }, 2000);
    }
    stop() {

    }
}